/**
 * Private Variable Declaration
 * */
 import Modules.Security.UserDetail;
 
 import com.desidea.common.CommonClass;
 import com.desidea.components.grid.SuperGridEvent;
 import com.desidea.components.grid.button.SuperGridButton;
 import com.desidea.components.spinner.BusySpinner;
 import com.desidea.webservice.PartnerService;
 import com.desidea.webservice.ProcessServiceRequestResultEvent;
 
 import flash.events.Event;
 
 import mx.controls.Alert;
 import mx.controls.Button;
 import mx.controls.dataGridClasses.DataGridColumn;
 import mx.core.Application;
 import mx.managers.PopUpManager;
 import mx.rpc.events.FaultEvent;
 
/**
 * Event Handling - CreationComplete event
 * */
private function windowCreated():void{

	BusySpinner.getInstance().play(Application.application.ModuleCvs);	
	var newItemBtn:Button = new Button();
	newItemBtn.label = "Create New User";
	newItemBtn.addEventListener(MouseEvent.CLICK, function(event:Event):void{
														showDetail(SuperGridButton.MODE_EDIT,"");
													});
	usersGrid.toolBar.addChild(newItemBtn);
	this.usersGrid.addEventListener(SuperGridEvent.CLICKED, gridButtonColumnClicked);
	loadData();
}

public function loadData():void{
	var params:String = "OPERATION=USERS_LIST" +
						"&ISACTIVE=X";
//						"&USERID="+CommonClass.UserID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForMyContactsList);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
}

private function callForMyContactsList(event:ProcessServiceRequestResultEvent):void{
	var response:XML = XML(event.result);

	lblError.text = "";
	if(response.RESPONSE.RESULT == "E"){
		lblError.text = response.RESPONSE.MESSAGE
	}else if(response.RESPONSE.RESULT == 'S'){
		this.usersGrid.dataProvider = response;
	}
	setColumnWidths();
	BusySpinner.getInstance().stop();	
}

private function partnerServiceFaultHandler(event:FaultEvent):void{
	BusySpinner.getInstance().stop();
	Alert.show(event.fault.toString());
}

private function setColumnWidths():void{
	CommonClass.setColumnWidth(this.usersGrid,1, 80, "right");
	CommonClass.setColumnWidth(this.usersGrid,2, 150);
	CommonClass.setColumnWidth(this.usersGrid,3, 100);
	CommonClass.setColumnWidth(this.usersGrid,4, 200);
	CommonClass.setColumnWidth(this.usersGrid,5, 140,"center");
	CommonClass.setColumnWidth(this.usersGrid,6, 120,"center");
	CommonClass.setColumnWidth(this.usersGrid,7, 90,"center");
	CommonClass.setColumnWidth(this.usersGrid,8, 90,"center");
}

private function gridButtonColumnClicked(event:SuperGridEvent):void{
	handleGridButtonClick(event);
}

private function handleGridButtonClick(event:SuperGridEvent, moduleCode:String = null):void{
	var mode:String =  String(event.data.linkId).toLowerCase();
	
	if(mode == SuperGridButton.MODE_EDIT || mode == SuperGridButton.MODE_VIEW || mode == SuperGridButton.MODE_VIEWICON){
		showDetail(mode, event.data.data["key"].toString());
	}else{
		Alert.show("Mode : " + mode + " not handled");
	}
}

private function showDetail(mode:String, key:String=""):void{
	var cd:UserDetail = new UserDetail();
	cd.key = key;
	cd.caller = this;
	
	PopUpManager.addPopUp(cd,this,true);
	PopUpManager.centerPopUp(cd);
}